<?php

class DetailController extends Controller
{
    /**
     * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
     * using two-column layout. See 'protected/views/layouts/column2.php'.
     */
    public $layout='//layouts/column2';

    /**
     * @return array action filters
     */
    public function filters()
    {
        return array(
			'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * Specifies the access control rules.
     * This method is used by the 'accessControl' filter.
     * @return array access control rules
     */
    public function accessRules()
    {
        return array(
        array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('index','view','createFromMobile'),
				'users'=>array('*'),
        ),
        array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('create','update'),
				'users'=>array('@'),
        ),
        array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete','add','ajaxAdd','getForEditing','massAdd',"massAddResults"),
				'users'=>array('@'),
        ),
        array('deny',  // deny all users
				'users'=>array('*'),
        ),
        );
    }

    public function actionMassAdd($match_id){
        $model = Match::model()->findByPk($match_id);
        if(!$_POST["details"]){
            $this->render("massAdd",array("model"=>$model));
        }else {
            $details = split("\n",$_POST["details"]);
            $right = "";
            $wrong = "";
             
            foreach ($details as $detail){
                if($detail !=""){
                    $fields = split(",",$detail);
                    $d = new Detail();
                    $d->match_id = $match_id;
                    $fields[0]<=count(Detail::$TYPES) ?$d->type = trim($fields[0]) : $d->type=-1;
                    $d->min = trim($fields[1]);
                    $d->player = trim($fields[2]);
                    $d->comment = trim($fields[3]);
                    $fields[4] <= count(Detail::$SIDE) ? $d->side = trim($fields[4]) :$d->side = -1;
                    if($d->save()){
                        $right .= $detail."<br />";
                    } else {
                        $wrong .= $detail."<br />";
                    }
                }
            }
            if ($right!=""){
                $model->save();
            }
            $this->render("massAddResults",array("right"=>$right,"wrong"=>$wrong,"match_id"=>$match_id));
        }
    }
    public function actionCreateFromMobile($id=-1,$match_id = -1, $pass="",$type=-1,$side=-1,$player = "",
    $minute = "", $score = ""){
        $cr = new CDbCriteria();
        $cr->condition = "name = 'Pass'";
        $setting = Settings::model()->find($cr);
         
        header("Content-type: text/xml");
        $xml_output = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
        if($pass == $setting->value){
            if($id==-1){
                $detail = new Detail();
            } else{
                $detail = Detail::model()->findByPk($id);
            }
            if($match_id == -1){
                $xml_output.= "<create response=\"Λάθος Αγώνας\" />";
            }else  if($player == ""){
                $xml_output.= "<create response=\"Λάθος παίκτης\" />";
            }else if($minute == "" ){
                $xml_output.= "<create response=\"Λάθος λεπτό\" />";
            }else if($type ==-1 ){
                $xml_output.= "<create response=\"Λάθος λεπτομέρεια\" />";
            }else if($side ==-1 ){
                $xml_output.= "<create response=\"Λάθος πλευρά\" />";
            }else{
                $detail->type = $type;
                $detail->match_id = $match_id;
                $detail->side = $side;
                $detail->player = $player;
                $detail->min = $minute;
                $detail->comment = $score;

                if($detail->save()){
                    if($this->updateMatch($match_id)){
                        if($id==-1){
                            $xml_output.= "<create response=\"Η λεπτομερεια δημιουργήθηκε\" />";
                        } else{
                            $xml_output.= "<create response=\"Η λεπτομερεια μετατράπηκε\" />";
                        }
                    }else{
                        $xml_output.= "<create response=\"Η λεπτομερεια μετατράπηκε/δημιουργήθηκε
                         αλλά ο αγώνας δεν ενημερώθηκε\" />";
                    }
                }else{
                    $xml_output.= "<create response=\"Κάποιο σφάλμα προέκυψε\" />";
                }
            }

        }else{
            $xml_output.= "<create response=\"Λάθος συνθηματικό\" />";
        }
        echo $xml_output;
         
    }
    public function actionAjaxAdd(){
        // echo $_POST["_id"];
        $model = Detail::model()->findByPk($_POST["Detail"]["_id"]);
        // CVarDumper::dumpAsString($model,2,true);exit;
        if($model == null){
            $model = new Detail();
        } else {
            $model->scenario = "Update";
        }
        $model->attributes = $_POST["Detail"];
        if($model->save()){
            $this->updateMatch($model->match_id);
            $models= $this->getMatchDetails($model->match_id);
            $out = "<div id='timeline'>";
            $out .= $this->renderPartial('_timeline',array('models'=>$models,'edit'=>true),true,true);
            $out .= "</div>";
            echo $out;
        }else{
            //CVarDumper::dump($model->attributes,2,TRUE);
        }
    }

    public function actionGetForEditing(){
        $id= $_POST["id"];
        $model = Detail::model()->findByPk($id);
        echo $this->renderPartial('_form',array('model'=>$model),true,true);
    }




    public function actionAdd($match_id){
        $match = Match::model()->findByPk($match_id);
        $this->render('create',array('models'=>$match->details,'match'=>$match));
    }
    private function updateMatch($match_id){
        $match = Match::model()->findByPk($match_id);
        if($match!=null){
            $match->updated = time();
            return $match->save();
        }
        return false;
    }
    private function getMatchDetails($match_id){
        $cr = new CDbCriteria();
        $cr->condition = "match_id=".$match_id;
        $cr->order = "min ASC";
        return Detail::model()->findAll($cr);
    }


}
